package org.proddevplm.helpers;

import org.proddevplm.dao.data.diagram.DiagramEntity;
import org.proddevplm.dao.data.diagram.EntityStructure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DiagramEntityBuilder {


    private static String test =
            "\"{\"njndefji\":{\"identifier\":\"njndefji\",\"type\":1,\"label\":\"Hactivitate\",\"x\":196,\"y\":156,\"an\":\"none\",\"asd\":\"04/01/2011\",\"aed\":\"04/15/2011\",\"ar\":\"Cristian Codre\",\"aa\":\"Radu Racasan\",\"sdp\":\"Concept Enhancement\"},\"anehmi\":{\"identifier\":\"anehmi\",\"type\":3,\"label\":\"databaza\",\"x\":460,\"y\":148,\"an\":\"activitate\",\"asd\":\"04/05/2011\",\"aed\":\"04/15/2011\",\"ar\":\"Cristian Codre\",\"aa\":\"Timea Gyorke\",\"sdp\":\"Analysis Stage\"},\"klkeami\":{\"identifier\":\"klkeami\",\"type\":0,\"from\":\"njndefji\",\"to\":\"anehmi\",\"dashed\":0},\"kdkdefmi\":{\"identifier\":\"kdkdefmi\",\"type\":1,\"label\":\"Label nou\",\"x\":310,\"y\":314,\"an\":\"none\",\"asd\":\"04/19/2011\",\"aed\":\"04/22/2011\",\"ar\":\"Cristian Codre\",\"aa\":\"Timea Gyorke\",\"sdp\":\"Analysis Stage\"},\"mhheiii\":{\"identifier\":\"mhheiii\",\"type\":0,\"from\":\"anehmi\",\"to\":\"kdkdefmi\",\"label\":\"Cu label\"}}\"";


    public static EntityStructure getEntityStructure(String json) {
        EntityStructure structure = new EntityStructure();
        List<Map<String, String>> links = new ArrayList<Map<String, String>>();

        List<Map<String, String>> objects = JSONParser.parse(json);
        for (Map<String, String> object : objects) {
            if (object.keySet().contains("from")) {
                links.add(object);
            } else {
                DiagramEntity entity = new DiagramEntity();
                entity.setIdentifier(object.get("identifier"));
                entity.setActivityResponsible(object.get("ar"));
                entity.setActivityAppriver(object.get("aa"));
                entity.setActivityName(object.get("an"));
                entity.setActivityStartDate(object.get("asd"));
                entity.setActivityEndDate(object.get("aed"));
                entity.setSelDesignPhase(object.get("sdp"));
                entity.setType(object.get("type"));
                entity.setToolName(object.get("st"));
                entity.setToolControlString(object.get("stHidden"));
                entity.setFilePath(object.get("fp"));

                structure.addEntity(entity);
            }
        }
        String from;
        String to;
        for (Map<String, String> object : links) {
            from = object.get("from");
            to = object.get("to");
            if (from==null || to==null || structure.getEntity(from)==null || structure.getEntity(to)==null)
            	continue;
            structure.getEntity(from).getNext().add(to);
            structure.getEntity(to).getPrevious().add(from);

        }

        return structure;
    }

    public static void main(String[] args) {
        DiagramEntityBuilder.getEntityStructure(test);
    }

}
